package com.lwm.lambda;

import com.lwm.entity.Employee;
import com.lwm.jiekou.MyPredicate;
import com.lwm.jiekou.jiekouImpl.FilterByAge;
import org.junit.Test;
import org.omg.PortableInterceptor.INACTIVE;

import java.awt.*;
import java.util.*;
import java.util.List;

public class TestLambda {

    //匿名内部类
    @Test
    public void test01(){
        Comparator<Integer> comparator = new Comparator<Integer>(){
            @Override
            public int compare(Integer o1, Integer o2) {
                return Integer.compare(o1, o2);
            }
        };
    }
    //lambda表达式
    @Test
    public void test02(){
        Comparator<Integer> comparator = (x, y)->Integer.compare(x, y);
        TreeSet<Integer> set =new TreeSet<>(comparator);
    }
    //不好
    @Test
    public void test03(){
        List<Employee> employeeList = Arrays.asList(
                new Employee("张三", 23, 6433),
                new Employee("张三1", 33, 4433),
                new Employee("张三2", 63, 65333),
                new Employee("张三3", 43, 5333),
                new Employee("张三4", 36, 4233)
        );
        List<Employee> employees = new ArrayList<>();
        for(Employee e:employeeList){
            if (e.getAge()>=35){
                employees.add(e);
            }
        }
    }
    //改进1： 使用设计模式----------=>策略设计模式------》普通实现
    @Test
    public void test04(){
        List<Employee> employeeList = Arrays.asList(
                new Employee("张三", 23, 6433),
                new Employee("张三1", 33, 4433),
                new Employee("张三2", 63, 65333),
                new Employee("张三3", 43, 5333),
                new Employee("张三4", 36, 4233)
        );
         List<Employee> employeeList1 = filterEmployee(employeeList, new FilterByAge());
         for (Employee e:employeeList1){
             System.out.println(e);
         }
    }
    //改进2： 匿名内部类
    public void test05(){
        List<Employee> employeeList = Arrays.asList(
                new Employee("张三", 23, 6433),
                new Employee("张三1", 33, 4433),
                new Employee("张三2", 63, 65333),
                new Employee("张三3", 43, 5333),
                new Employee("张三4", 36, 4233)
        );
        List<Employee> list = filterEmployee(employeeList, new MyPredicate<Employee>() {
            @Override
            public boolean test(Employee employee) {
                if(employee.getSalary()>= 5000)
                    return true;
                return false;
            }
        });
//        List<Employee> list = filterEmployee(employeeList, new MyPredicate<Employee>() {
//            @Override
//            public boolean test(Employee employee) {
//                if(employee.getAge()>= 35)
//                    return true;
//                return false;
//            }
//        });
        /**
         * 提前使用一下lambda表达式
         */
        list.forEach(System.out::println);
    }
    //改进3：lambda表达式
    @Test
    public void test06(){
        List<Employee> employeeList = Arrays.asList(
                new Employee("张三", 23, 6433),
                new Employee("张三1", 33, 4433),
                new Employee("张三2", 63, 65333),
                new Employee("张三3", 43, 5333),
                new Employee("张三4", 36, 4233)
        );
        List<Employee> list = filterEmployee(employeeList, (e)->e.getSalary()>5000);
        list.forEach(System.out::println);
    }
    //改进4：StreamAPI
    @Test
    public void test07(){
        List<Employee> employeeList = Arrays.asList(
                new Employee("张三", 23, 6433),
                new Employee("张三1", 33, 4433),
                new Employee("张三2", 63, 65333),
                new Employee("张三3", 43, 5333),
                new Employee("张三4", 36, 4233)
        );
        employeeList.stream().filter((employee) -> employee.getSalary()>5000).forEach(System.out::println);
    }
    public List<Employee> filterEmployee(List<Employee> emp, MyPredicate<Employee> filterEmployee){
            List<Employee> employees = new ArrayList<>();
            for(Employee e:emp){
                if (filterEmployee.test(e)) {
                    employees.add(e);
                }
            }
            return employees;
    }

}
